package com.tongjie.hotelbooking.security.service;


import com.tongjie.hotelbooking.security.user.CurrentUserInfo;
import com.tongjie.hotelbooking.user.entity.User;
import com.tongjie.hotelbooking.user.service.UserService;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;

import javax.annotation.Resource;

public class JpaUserDetailsService implements UserDetailsService {

    @Resource
    private UserService userService;

    @Resource
    private PasswordEncoder passwordEncoder;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        User user = userService.getUserByUsername(username);

        if (user == null) {
            throw new UsernameNotFoundException(username);
        }

        return CurrentUserInfo.builder().username(user.getUsername()).password(user.getPassword()).build();
    }
}
